package com.yycx.module.bus.provider.handler.haiying.tasktodo.calculate;

import com.yycx.bpm.provider.listener.FlowPageInterceptor;
import com.yycx.bpm.provider.model.TaskResponse;
import com.yycx.common.base.entity.EntityMap;
import com.yycx.common.base.service.BaseAdminUserService;
import com.yycx.common.base.utils.FlymeUtils;
import com.yycx.common.security.OpenHelper;
import com.yycx.module.bus.provider.service.hy.HyOrderService;
import com.yycx.module.system.client.entity.SysCompany;
import com.yycx.module.system.provider.service.SysCompanyService;
import org.flowable.common.engine.api.query.Query;
import org.flowable.task.api.TaskInfoQuery;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 费用核算-我的待办
 *
 * @author zyf
 */
@Component
public class HyCalApproveTab1ListHandler implements FlowPageInterceptor<TaskResponse> {

    @Resource
    private HyOrderService orderService;
    @Resource
    private SysCompanyService companyService;


    @Override
    public void prepare(Query query, EntityMap params) {
        TaskInfoQuery taskQuery = (TaskInfoQuery) query;
        String orderNo = params.get("orderNo");
        String beginDate = params.get("beginDate");
        String endDate = params.get("endDate");
        String deptId = params.get("deptId");
        String deptType = params.get("deptType");
        Long companyId = OpenHelper.getCompanyId();
        if (FlymeUtils.isNotEmpty(orderNo)) {
            taskQuery.processVariableValueLike("orderNo", orderNo);
        }
        if (FlymeUtils.isNotEmpty(beginDate)) {
            taskQuery.processVariableValueGreaterThanOrEqual("sendDate", beginDate);
        }
        if (FlymeUtils.isNotEmpty(endDate)) {
            taskQuery.processVariableValueLessThanOrEqual("sendDate", endDate);
        }
        if (FlymeUtils.isNotEmpty(deptId)) {
            taskQuery.processVariableValueEquals("deptId", deptId);
        }
        if (FlymeUtils.isNotEmpty(deptType)) {
            taskQuery.processVariableValueEquals("deptType", deptType);
        }
        taskQuery.processVariableValueEquals("companyId", companyId);
        //排除发起人节点
        taskQuery.taskDefinitionKeyLike("Activity_%");
        //关联租户ID
        taskQuery.taskTenantId("flp").orderByTaskCreateTime().desc();
        //关联流程key
        taskQuery.processDefinitionKey("process_settle_apply_flp");
    }

    @Override
    public List<EntityMap> complete(Query query, List<TaskResponse> data, EntityMap params) {
        List<EntityMap> list = new ArrayList<>();
        if (FlymeUtils.isNotEmpty(data)) {
            for (TaskResponse taskResponse : data) {
                EntityMap map = taskResponse.getResult();
                String orderId = taskResponse.getBusinessKey();
                //查询订单信息
                if (FlymeUtils.isNotEmpty(orderId)) {
                    EntityMap orderMap = orderService.getMapByOrderId(orderId);
                    if (FlymeUtils.isNotEmpty(orderMap)) {
                        Long companyId = orderMap.getLong("companyId");
                        SysCompany company = companyService.getById(companyId);
                        map.put("companyName", company.getCompanyName());
                        map.put("orderId", taskResponse.getBusinessKey());
                        map.putAll(orderMap);
                    }
                    list.add(map);
                }
            }
        }
        return list;
    }
}
